package com.etoak.car.controller;

import com.alibaba.fastjson.JSONObject;
import com.etoak.car.bean.Car;
import com.etoak.car.bean.Order;
import com.etoak.car.bean.Users;
import com.etoak.car.service.RentalService;
import com.etoak.commons.enums.ETEnums;
import com.etoak.commons.redis.RedisUtil;
import com.etoak.commons.response.ETResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.UUID;

@RestController
@RequestMapping("/rental")
public class RentalController {

    @Autowired
    RentalService rentalService;

    /**
     * 订单
     * @return
     */
    @RequestMapping("/rentalCar")
    public ETResponse rentalCar(int carid,String token){
        ETResponse response = new ETResponse();

        Car car = rentalService.getCarById(carid);
        if(!ObjectUtils.isEmpty(car)){

            if(car.getStatus()==1){
                response.setCode(ETEnums.ERROR.getCode());
                response.setMsg(ETEnums.ERROR.getMsg());
                response.setData("此车已经出租");
                return response;
            }else {
                //car.setStatus(1);

                //  从redis中取出用户数据
                RedisUtil ru = RedisUtil.getRedisUtil();
                String userInfo = ru.get(token);
                Users u = JSONObject.parseObject(userInfo, Users.class);
                System.out.println(u.getId());
                if(!ObjectUtils.isEmpty(u)){
                    if(car.getPrice()>u.getMoney()){
                        response.setCode(ETEnums.ERROR.getCode());
                        response.setMsg(ETEnums.ERROR.getMsg());
                        response.setData("余额不足，请充值");
                        return response;
                    }else {
                        rentalService.upStatus(carid);
                        int userid = u.getId();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        long time = System.currentTimeMillis();
                        String zudate = sdf.format(time);
                        Order order = new Order();
                        order.setCarid(carid);
                        order.setUserid(userid);
                        order.setZudate(zudate);

                        rentalService.rentalCar(order);

                        response.setCode(ETEnums.SUCCESS.getCode());
                        response.setMsg(ETEnums.SUCCESS.getMsg());
                        response.setData("成功");

                        return response;
                    }
                }else {
                    response.setCode(ETEnums.ERROR.getCode());
                    response.setMsg(ETEnums.ERROR.getMsg());
                    response.setData("用户token不对");

                    return response;
                }
            }
        }else {
            response.setCode(ETEnums.ERROR.getCode());
            response.setMsg(ETEnums.ERROR.getMsg());
            response.setData("汽车ID不对");

            return response;
        }

    }
}
